'''
Created on 22/06/2014

@author: Vitor e Monique
'''
from backPropagation.RedeNeural2Camadas import redeNeural
from funcoesEderivadas.Heaviside import heaviside, derivadaHeaviside
from funcoesEderivadas.criarSaidas import escreveSaida
import xlrd
from funcoesEderivadas.fazTestes import testa
from funcoesEderivadas.TangenteHiperbolica import tanHiper
from funcoesEderivadas.criarEntradas import criaEntrada16atrib,\
    criaEntrada8atrib, criaEntrada5atrib, criaEntrada24atrib

if __name__ == '__main__':
    '''
    #testa para porta XOR
    entradas = [[0,0],[0,1],[1,0],[1,1]]
    
    zDesejados = [0,1,1,0]
    
    
    omegasEntradaMeio = [[-0.5,0.5],[0.5,-0.5]]
    tetasMeio = [0.4,0.4]
    
    omegasMeioSaida = [0.5,-0.5]
    tetaSaida = -0.4
    
    
    listaErros, NEWomegasEntradaMeio, NEWomegasMeioSaida, NEWtetasMeio, NEWtetaSaida = redeNeural(entradas, zDesejados, omegasEntradaMeio, tetasMeio, omegasMeioSaida, tetaSaida, heaviside, derivadaHeaviside)
    
    escreveSaida("../arquivos/saida/", len(entradas[0]), len(tetasMeio), listaErros, NEWomegasEntradaMeio, NEWomegasMeioSaida, NEWtetasMeio, NEWtetaSaida)
    
    
    print("-------------------------------------------------------------------------------------")
    print("Teste Criação de Listas de Entrada e zDesejados\n\n")
    
    entradas, zDesejados, entradasTeste, zDesejadosTeste, omegasEntradaMeio, tetasMeio, omegasMeioSaida, tetaSaida = criaEntrada24atrib("../arquivos/entrada/dados.xlsx", 6) 
    
    arquivo = open("../arquivos/teste.txt", 'w')
    
    arquivo.write("Numero de Entradas: ")
    arquivo.write(str(len(entradas)))
    arquivo.write("\nTamanho das Entradas: ")
    arquivo.write(str(len(entradas[0])))
    arquivo.write("\nNumero de zDesejados: ")
    arquivo.write(str(len(zDesejados)))
    arquivo.write("\nNumero de Entradas Teste: ")
    arquivo.write(str(len(entradasTeste)))
    arquivo.write("\nNumero de zDesejados Teste: ")
    arquivo.write(str(len(zDesejadosTeste)))
    
    arquivo.write("\n\nEntradas: ")
    for i in range(len(entradas)):
        arquivo.write(str(entradas[i]) + "\n")
    
    arquivo.write("\n\nEntradas Teste: ")
    for i in range(len(entradasTeste)):
        arquivo.write(str(entradasTeste[i]) + "\n")
    
    print("Arquivo teste.txt criado!\n\n")
    
    '''
    
    print("-------------------------------------------------------------------------------------")
    print("Teste 3 - 16 entradas, 12 neuronios na Camada Intermediaria\n\n")
    
    entradas, zDesejados, entradasTeste, zDesejadosTeste, omegasEntradaMeio, tetasMeio, omegasMeioSaida, tetaSaida = criaEntrada16atrib("../arquivos/entrada/dados.xlsx", 12) 
    
    NEWomegasEntradaMeio =  [
                                [-0.48819467424610724, -0.40005176447512419, -0.42285189428206127, 0.077338247019817064, 0.19620857362634309, 0.16578834511995477, -0.080766286825911596, 0.080979365719010404, -0.061840039564954798, 0.078232006100447227, -0.038918268620328156, 0.16664925682396195],
                                [-0.095497924920730184, -0.0055988624473577589, -0.36713627546895361, -0.49701400612560104, 0.11938593167902582, 0.27707742757974979, 0.087914117850980342, -0.41313319599937881, 0.21035030025571702, 0.2327512352336879, 0.4701414170391765, 0.4108597762262266],
                                [0.51136740678267234, 0.39510623521209109, 0.45776950082145285, -0.16265795001198724, -0.045352348996651463, -0.16178868524973702, 0.024297643086156642, 0.27499266194562622, -0.15995144316591775, -0.21359856553635209, -0.017752666550373875, -0.35765473676925502],
                                [0.049540924402564052, -0.31538481963400156, 0.46552722409756048, -0.33045532031189762, -0.013752314236217065, -0.10849466353141034, -0.15416796705702862, 0.0914907072008663, -0.037270297606251321, -0.46574620006846806, -0.38078211743563373, 0.4359092373954031],
                                [0.20294344082922205, -0.035541016414112496, 0.20565777238204316, 0.28541503653908934, -0.089785530856713466, -0.21769343715054496, -0.44690048794614251, -0.41154681786518144, -0.097400412242460224, 0.43489424686515954, -0.10621638183273105, 0.24695109346188646],
                                [0.16718343375103686, 0.40308019162451408, 0.42869957617032145, -0.18318228020749175, 0.049252655606028047, -0.3370095135310554, 0.50351705299707905, 0.1142171763022807, -0.052285520571045865, 0.31462646193870331, 0.15104414369560004, 0.17152812225557859],
                                [0.30135973442147446, 0.038019057421335385, -0.35959657187508887, 0.064884366955684558, -0.0770064766244698, -0.31942223183868723, 0.27374777346840418, 0.15225455718526112, -0.11784500103326818, -0.3644327982927954, 0.073321432145280016, 0.037112796282153866],
                                [0.50015536342719946, 0.45171404629449136, -0.34424641368038722, 0.23078198033059011, -0.38708623899454592, 0.31641925621559203, 0.038680822213641344, 0.48655691498992132, 0.30254499615926922, 0.063486036083387293, 0.063492155800993211, 0.058756790465948977],
                                [0.063723809837640283, 0.18510941910106707, 0.16292978827263549, -0.52876570345253771, -0.084092463819416499, -0.1936094367570308, 0.036420121186868992, -0.076599182311394717, 0.47444250689235284, -0.067201295500583944, -0.3476934958146764, 0.24874177498579272],
                                [0.14636612662121873, -0.24760010921587039, -0.0349650330224043, -0.062234730839399323, 0.044720128270963382, -0.4664711927444169, -0.6783563059491905, -0.11289857634862097, -0.19430555801649735, -0.22061125893431308, 0.30138307627669775, -0.06362093466010478],
                                [0.0051141116716982276, -0.093652578394567532, -0.16990742391490915, -0.27782025350871359, 0.039019716287363483, 0.19987912030103569, 0.076648299882118326, 0.030940064839582275, 0.12516341383258711, -0.37380798108186458, -0.039703792798478688, -0.43974602500343157],
                                [-0.12275076157681637, -0.050977996605352661, -0.42053286342381335, -0.0037224800461867196, 0.1652880440780618, -0.21958035125799577, 0.083003272202888154, -0.22823534559987657, 0.010641310109336248, 0.074730707412094602, -0.16049349783550645, -0.12273910711100046],
                                [0.34245820161369056, 0.42069208061702101, -0.39040964269834932, -0.073361001643296203, -0.036299064938810001, 0.38913778047505643, 0.16397235104179439, -0.5172060874860438, 0.30436639344415256, -0.016980341942614881, 0.073102483995284753, 0.2337347498842654],
                                [-0.12921443616746078, -0.1516188361281533, 0.29197739341896434, -0.028743070987448369, -0.0026730636581376961, 0.16952429571273844, 0.002995021904975765, 0.43913602385677769, -0.16018935403953569, -0.17716070746556212, 0.24243535984365719, 0.11697590863321486],
                                [-0.43595965728146047, 0.22789392354305504, -0.20882678080699488, -0.13244811367334813, 0.024289393792521083, -0.040717083337707516, 0.11262306969658498, 0.018510831686918031, -0.023779193511408489, 0.26437914994115186, 0.05326443754100961, -0.29436219144811415],
                                [-0.35335862216189368, -0.10569360248848374, 0.30729369210046065, -0.1266954616077004, -0.32507424751574188, 0.37256297672068972, 0.29962516580829979, -0.2515704557964355, 0.32196083901344408, -0.32965792968641, -0.38998114635852227, -0.45379157768884804]
                            ]
    
    NEWomegasMeioSaida =    [
                                -0.00566683772,
                                0.414995863831,
                                -0.211667307622,
                                -0.128261053772,
                                0.817533025013,
                                1.11992766789,
                                -0.041821536063,
                                0.164754376694,
                                0.0568577707033,
                                0.369689747407,
                                -0.313385061558,
                                0.165584223379
                            ]
    
    NEWtetasMeio =  [
                        -7.65842860816,
                        -7.83963791099,
                        -8.20980888908,
                        -7.9919907775,
                        -7.47681391664,
                        -7.57909537828,
                        -7.79858338819,
                        -8.21018100782,
                        -8.24746114911,
                        -8.32259637366,
                        -7.89887934581,
                        -8.22790082336
                    ]
    
    NEWtetaSaida =  [
                        2.41019673193
                    ]
    
    testa(len(entradas[0]), entradasTeste, zDesejadosTeste, NEWomegasEntradaMeio, NEWomegasMeioSaida, NEWtetasMeio, NEWtetaSaida, tanHiper)
    
    print("Fim testes!\n\n")
    
    print("-------------------------------------------------------------------------------------")
    print("Teste 3 - 16 entradas, 18 neuronios na Camada Intermediaria\n\n")
    
    entradas, zDesejados, entradasTeste, zDesejadosTeste, omegasEntradaMeio, tetasMeio, omegasMeioSaida, tetaSaida = criaEntrada16atrib("../arquivos/entrada/dados.xlsx", 18) 
    
    NEWomegasEntradaMeio =  [
                                [-0.19264375328789404, 0.33532589569510446, -0.15480712809042166, -0.12926640241958651, -0.13859237879418157, -0.27102757782470965, -0.096982297923559585, -0.45463254190137498, 0.28427735509094465, 0.47719471338659869, -0.097942378020584323, 0.4786061759270594, 0.48786890232282382, 0.23978525599878164, 0.41566175089793728, 0.071947316198531175, -0.31836898941441644, 0.047551876939520288],
                                [0.23971806955890582, 0.38045505966547077, -0.34934321793472095, -0.27229338022733179, 0.12000995025672806, 0.24616850377341171, -0.2536055283953042, -0.27853605005968607, 0.35142489415532879, 0.12936632594617914, 0.27253620290218922, 0.25497926311329244, -0.11644239156354329, 0.1074160108101328, -0.13717063835565638, -0.056244680467986988, 0.19654824718332592, -0.25331468970888127],
                                [0.43514855491703042, 0.47963275612413453, -0.20455380247138469, 0.0022903936343973284, 0.06931451095411767, -0.034376725888518832, -0.30948822600690562, -0.28555359208454889, 0.25741653110555468, -0.42384109995666269, -0.19903060601127651, 0.42534454925604348, -0.061509034425107315, 0.29449212193127844, 0.054935074306433247, -0.40864917585585858, 0.24405226880842484, 0.18612779327863771],
                                [-0.19446910181969615, 0.051666702866941097, -0.032261059055727549, -0.42600879558763316, -0.07794462963573974, 0.17285224641810382, 0.051673637467715944, -0.28511613907486177, 0.4326537804774116, 0.24820013912712147, 0.41327488388858291, -0.0061928298247013779, 0.22986300893229716, -0.32387023716141339, 0.10000310989918586, 0.14212413010716687, -0.0021748528473049021, -0.24607862458515772],
                                [-0.11385778210021856, -0.17665939102529127, 0.2328836254372619, 0.40031052822613106, 0.41261728395382224, 0.17001197873622217, 0.083220079956895857, -0.37262702514566126, -0.207536299067522, -0.30745809983266037, -0.054835258820171075, -0.31156690288586042, -0.36737074225084548, 0.27753228816966558, -0.13147853815075605, 0.27900708353929604, 0.31603267926281947, 0.20685798327329391],
                                [-0.035771568351955475, 0.32295333970959533, 0.32923395989963117, 0.040425842343560947, 0.46367425374343219, -0.10745440784770553, 0.26132042642953501, 0.19531761083188001, -0.32628198324151853, -0.47302385312128425, 0.29491093849618655, 0.42002544151276472, -0.12789052344914476, -0.44005506680317796, 0.30459229104836871, 0.35219778595023604, 0.24781836862045292, -0.031695582297656927],
                                [0.24474634068195814, -0.05715138516807327, -0.40467290102646125, 0.034999061784226779, 0.11146799998095538, 0.3651782437957406, 0.116893171507924, -0.25375478183386518, -0.48323143480900282, 0.10534434040774472, 0.10548095647013704, 0.32744811668306745, -0.49146844608550777, 0.074285819224889429, -0.25575329088162152, 0.26525101537045059, -0.19239505209783378, 0.41059520035330077],
                                [-0.33526584351403216, 0.44073522681143551, -0.29981329656989025, 0.46647267570421902, -0.3777352806819298, -0.0016092714389981367, -0.048144842135599428, -0.17402192978697129, -0.27758087687067756, -0.37331345188224674, -0.33408419474828488, -0.24378641312166816, -0.13003740507259662, -0.2624640959883659, 0.15932728368710869, -0.00033861278776992217, -0.45025631661187965, 0.25165026657888168],
                                [-0.19138900121792368, -0.20989766821928385, 0.22323453738018431, -0.35797923030606543, 0.12464608731973507, 0.25871318777668451, -0.20385441269900126, 0.26858728820230787, 0.051406719677249073, 0.14438320661941542, 0.39290704426942191, 0.16411736375376559, 0.53077257137672651, 0.15122316943790035, 0.32608932032528398, 0.21988802201812324, -0.13637040405904638, 0.25888800666481826],
                                [-0.16742972415663943, 0.28767680008891694, 0.49901219568195559, -0.0035956751170445811, 0.13472539581413437, 0.1231016148634277, -0.012716441544075472, -0.41653887546265483, 0.28411750832415228, 0.26199401843481573, 0.0077315050674673161, 0.35043524056678188, -0.47786999264116692, 0.22534821202777514, -0.072702097258368695, -0.32952524707946407, 0.28504699782474796, -0.14797674559753007],
                                [-0.074141114029650221, -0.26537084184962906, -0.17309578545718121, 0.022120897485634772, -0.30352081605907166, 0.31357344008606602, 0.27081290161264471, -0.17287578506572118, -0.061477024271743758, 0.34450235520267453, -0.11200721889542262, 0.076718947314547856, 0.41438017816498102, 0.052506111554756221, 0.016235100158932623, -0.47955965691517499, -0.35843576663002746, -0.086246774216537037],
                                [0.13533174886389338, -0.11635489654128175, 0.084111943161522679, -0.42948772518309264, 0.4414102538682303, -0.13600202621388316, -0.29364695185459844, 0.40721484893759696, -0.029024490195230856, 0.1314292230179944, -0.1072602966852228, 0.096367300108856302, 0.14030025642677163, -0.46065620482638231, -0.41901050425857517, 0.058519713069334345, 0.055037187387100832, 0.59338382233626819],
                                [0.33384069101572594, 0.1623879557463955, -0.34348024124735593, -0.20398725055116854, -0.53188118024751463, -0.33999624682851298, 0.43487929900132505, -0.086912936373322244, -0.0033302701385280382, 0.28896079738620611, 0.23589498231021341, -0.31659730472316455, -0.35855843469093079, 0.095368731318967198, 0.14314717119809306, -0.053886185636805119, 0.12234658385479426, 0.0013727377324252892],
                                [-0.25411470346251536, 0.13124801735756078, 0.15331383401118376, 0.43004748724056985, -0.25959273158320195, -0.051209850590562787, 0.021821405562841047, -0.0030308678612209683, -0.43212467460570747, -0.16991450566081737, -0.43045609108792265, 0.03486951610889847, -0.38945819377859658, -0.066189598535585106, -0.29628325488322538, -0.50352351546104346, 0.35038680655990267, -0.18510789637452416],
                                [0.10851806197393905, -0.0069129346399331255, -0.29469358272670748, -0.0056697553295155792, 0.21291293142552525, -0.093471886490047007, 0.21163320623650872, 0.26660397742626291, 0.22693198442645729, -0.073784723120905199, 0.12043592620114452, -0.06918479698822233, -0.31962961225868758, 0.013304556138077978, 0.0064920570273021179, 0.012612256002060229, -0.41943433792176638, 0.28105672514082169],
                                [0.45224326137119286, -0.02706041176071923, 0.2792227573759764, 0.22109065878545259, 0.14328245433947809, -0.39656948759818639, -0.3709437981146923, -0.27331084556796215, -0.14538829143607726, -0.47290834667493226, 0.35275096797887062, -0.10576321367077189, -0.44808638064956169, -0.19201436392896495, -0.24708459801016594, -0.27526003229698176, 0.23145385142456368, 0.43345943639436274]
                            ]
    
    NEWomegasMeioSaida =    [
                                0.639588809647,
                                0.126667153107,
                                -0.0891975975896,
                                0.0160396674182,
                                0.0311631743823,
                                -0.552161945807,
                                -0.584701712914,
                                -0.0139957761931,
                                -0.168040918322,
                                -0.355729439169,
                                0.126220513674,
                                -0.275523994595,
                                -0.064858516284,
                                -0.845476731738,
                                0.0828067820248,
                                0.311056077335,
                                0.433420402433,
                                0.130913230049
                            ]
    
    NEWtetasMeio =  [
                        -8.16493796092,
                        -8.11715161653,
                        -8.6313141439,
                        -8.64241144899,
                        -7.849626391,
                        -8.06237373402,
                        -8.76669302756,
                        -7.96278803961,
                        -7.88159196063,
                        -8.34957519502,
                        -8.13601447256,
                        -8.12892743892,
                        -8.60953280155,
                        -8.02967617301,
                        -8.72054688007,
                        -8.54193658855,
                        -8.08340225156,
                        -8.58032156958
                    ]
    
    NEWtetaSaida =  [
                        -1.05281896245
                    ]
    
    testa(len(entradas[0]), entradasTeste, zDesejadosTeste, NEWomegasEntradaMeio, NEWomegasMeioSaida, NEWtetasMeio, NEWtetaSaida, tanHiper)
    
    print("Fim testes!\n\n")
    
    '''
    print("-------------------------------------------------------------------------------------")
    print("Teste 4 - 24 entradas, 16 neuronios na Camada Intermediaria\n\n")
    
    entradas, zDesejados, entradasTeste, zDesejadosTeste, omegasEntradaMeio, tetasMeio, omegasMeioSaida, tetaSaida = criaEntrada24atrib("../arquivos/entrada/dados.xlsx", 16) 
    
    NEWomegasEntradaMeio =  [
                              
                            ]
    
    NEWomegasMeioSaida =    [
                          
                            ]
    
    NEWtetasMeio =  [
                    
                    ]
    
    NEWtetaSaida =  [
                    
                    ]
                    
    testa(len(entradas[0]), entradasTeste, zDesejadosTeste, NEWomegasEntradaMeio, NEWomegasMeioSaida, NEWtetasMeio, NEWtetaSaida, tanHiper)
    
    print("Fim testes!\n\n")
    
    print("-------------------------------------------------------------------------------------")
    print("Teste 4 - 24 entradas, 20 neuronios na Camada Intermediaria\n\n")
    
    entradas, zDesejados, entradasTeste, zDesejadosTeste, omegasEntradaMeio, tetasMeio, omegasMeioSaida, tetaSaida = criaEntrada24atrib("../arquivos/entrada/dados.xlsx", 20) 
    
    NEWomegasEntradaMeio =  [
                              
                            ]
    
    NEWomegasMeioSaida =    [
                          
                            ]
    
    NEWtetasMeio =  [
                    
                    ]
    
    NEWtetaSaida =  [
                    
                    ]

    testa(len(entradas[0]), entradasTeste, zDesejadosTeste, NEWomegasEntradaMeio, NEWomegasMeioSaida, NEWtetasMeio, NEWtetaSaida, tanHiper)
    
    print("Fim testes!\n\n")
    '''
    pass